package com.bjqg.bjqg01.bjqg.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ajguan.library.EasyRefreshLayout;
import com.bjqg.bjqg01.bjqg.R;
import com.bjqg.bjqg01.bjqg.adapter.PreEntrustNumberDetailAdapter;
import com.bjqg.bjqg01.bjqg.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/4/12 0012.
 */

public class PreEntrustNumberDetailActivity extends BaseActivity implements View.OnClickListener {

    private ImageView goback;
    private TextView toptitle;
    private ImageView select;
    private RecyclerView rvdetail;
    private EasyRefreshLayout refreshLayout;
    private PreEntrustNumberDetailAdapter preEntrustNumberDetailAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pre_entrust_num_detail);

        initView();
    }

    private void initView() {
        goback = (ImageView) findViewById(R.id.back);
        toptitle = (TextView) findViewById(R.id.tv_top_title);
        select = (ImageView) findViewById(R.id.select);
        rvdetail = (RecyclerView) findViewById(R.id.pre_entrust_detail_rv);
        refreshLayout = (EasyRefreshLayout) findViewById(R.id.refresh_pre_entrust_detail);

        goback.setOnClickListener(this);
        toptitle.setText("预委托编号详情");
        select.setVisibility(View.INVISIBLE);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvdetail.setLayoutManager(layoutManager);


        preEntrustNumberDetailAdapter = new PreEntrustNumberDetailAdapter(R.layout.item_pre_entrust_detail, generateTips());
        rvdetail.setAdapter(preEntrustNumberDetailAdapter);
        preEntrustNumberDetailAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Toast.makeText(getApplicationContext(),"预委托详情"+position,Toast.LENGTH_SHORT).show();
                ToastUtil.show(getApplicationContext(),"预委托详情"+position);
                Intent intent = null;
                //判断委托类型
                if (position%2==0) {
                    intent = new Intent(getApplicationContext(), EngineerEntrusActivity.class);
                    startActivity(intent);
                }else{
                    intent = new Intent(getApplicationContext(), OneSelfEntrusActivity.class);
                    startActivity(intent);
                }
            }
        });
        refreshLayout.addEasyEvent(new EasyRefreshLayout.EasyEvent() {
            @Override
            public void onLoadMore() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        refreshLayout.loadMoreComplete(new EasyRefreshLayout.Event() {
                            @Override
                            public void complete() {
                                preEntrustNumberDetailAdapter.getData().addAll(generateTips());
                                preEntrustNumberDetailAdapter.notifyDataSetChanged();

                            }
                        }, 500);

                    }
                }, 2000);
            }

            @Override
            public void onRefreshing() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        preEntrustNumberDetailAdapter.setNewData(generateTips());
                        refreshLayout.refreshComplete();
                        Toast.makeText(getApplicationContext(), "refresh success", Toast.LENGTH_SHORT).show();
                    }
                }, 1000);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
        }
    }

    private ArrayList<String> generateTips() {
        ArrayList<String> tips = new ArrayList<>();
        for (int i = 0; i < 5; i++) {

            tips.add("预委托详情:"+i);
        }

        return tips;
    }
}
